package com.gdut.huayan.utils;

import com.gdut.huayan.service.exception.BaseException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;

public class SecurityUtils {

    public static String getUserId() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof String && !"".equals(principal)) {
            return (String) principal;
        } else {
            throw new BaseException(4005, "您的令牌不存在或有误");
        }
    }

    public static Long getUserIdNumber() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof String && !"".equals(principal)) {
            return Long.parseLong((String) principal);
        } else {
            throw new BaseException(4005, "您的令牌不存在或有误");
        }
    }
    public static String getToken() {
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        if (credentials instanceof String && !"".equals(credentials)) {
            return (String) credentials;
        } else {
            throw new BaseException(4005, "您的令牌不存在或有误");
        }
    }


}
